﻿using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Controllers;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
using MyCode.Data.Models;
using MyCode.IOC.Impl;
using Microsoft.AspNetCore.Mvc.Filters;
using System;

namespace MyCode.Web
{
    public class Startup
    {
        public Startup(IConfiguration configuration)
        {
            Configuration = configuration;
        }

        public IConfiguration Configuration { get; }

        // This method gets called by the runtime. Use this method to add services to the container.
        public IServiceProvider ConfigureServices(IServiceCollection services)
        {

            services.AddSession();
            services.AddMemoryCache();//  注册缓存组建
            services.Configure<CookiePolicyOptions>(options =>
            {
                // This lambda determines whether user consent for non-essential cookies is needed for a given request.
                options.CheckConsentNeeded = context => false;
                options.MinimumSameSitePolicy = SameSiteMode.None;

            });
            services.AddMvc();
            services.Replace(ServiceDescriptor.Transient<IControllerActivator, ServiceBasedControllerActivator>());//注册IOC
            services.TryAddSingleton<IHttpContextAccessor, HttpContextAccessor>();//注册单例Http请求;

            var dbbuilder = new DbContextOptionsBuilder<NFineBaseContext>().UseSqlServer(Configuration.GetConnectionString("NFineBase"));//注册EF Core
            services.AddDbContext<NFineBaseContext>(options => options = dbbuilder); // 使用autofac 注入
            services.AddScoped<NFineBaseContext>((p) => { return new NFineBaseContext(dbbuilder.Options); });
            return IOCManagerImpl.Instance.Initialize(services);//将这个注册IOC 服务传到 IOC 容器中   要想实现注入 必须要把这个项目引用过来  不然 IOC 找不到
        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IHostingEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }
            else
            {
                app.UseExceptionHandler("/Home/Error");
                // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
                app.UseHsts();
            }

            app.UseHttpsRedirection();
            app.UseStaticFiles();
            app.UseCookiePolicy();
            app.UseSession();
            app.UseMvc(routes =>
            {
                routes.MapRoute(
                         name: "areas",
                         template: "{area:exists}/{controller=Home}/{action=Index}/{id?}");
                routes.MapRoute(
                    name: "default",
                    template: "{controller=Login}/{action=Index}/{id?}");

            });

        }
    }
}
